﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace DevExpress.MailClient.Win.Forms
{
    public partial class frmWorkFlowSelection : PRO_DOCS.BLL.WIN.Templates.DevxPermissionTemplate
    {
        private long CURRENT_SELECTED_DOCUMENT_TYPE_ID
        { get; set; }

        private Guid CURRENT_SELECTED_ARCHIVE_DEPARTMENT_FILE_ID
        { get; set; }

        private decimal CURRENT_SELECTED_APPLICATION_WORFLOW_ID
        { get; set; }

        public frmWorkFlowSelection()
        {
            InitializeComponent();
        }

        private void START_FILE_WORKFLOW()
        {

            PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENT_FILE_WORKFLOWSInsert
                (
                    Guid.NewGuid(),
                    CURRENT_SELECTED_APPLICATION_WORFLOW_ID,
                    CURRENT_SELECTED_ARCHIVE_DEPARTMENT_FILE_ID,
                    PRO_DOCS.BLL.WIN.Authentication.LoggedInEmployeeSession.CURRENT_LOGGED_IN_USER_SESSION.LOGIN_USER_ID.Value
                    );

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            //this.Close();
        }

        public frmWorkFlowSelection(long cURRENT_SELECTED_DOCUMENT_TYPE_ID, Guid cURRENT_SELECTED_ARCHIVE_DEPARTMENT_FILE_ID)
        {
            InitializeComponent();


            // TODO: Complete member initialization
            CURRENT_SELECTED_DOCUMENT_TYPE_ID = cURRENT_SELECTED_DOCUMENT_TYPE_ID;
            CURRENT_SELECTED_ARCHIVE_DEPARTMENT_FILE_ID = cURRENT_SELECTED_ARCHIVE_DEPARTMENT_FILE_ID;

            
        }

        private void gcApplicationWorkFlows_Click(object sender, EventArgs e)
        {

        }

        private void btnStartWorkflow_Click(object sender, EventArgs e)
        {
            if (gvApplicationWorkflows.FocusedRowHandle > -1)
            {
                START_FILE_WORKFLOW();
            }
            else
            {
                XtraMessageBox.Show("You've to choose  workflow before continue..", "Action denied", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void gvApplicationWorkflows_FocusedRowChanged(object sender, XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle > -1)
            {
                PRO_DOCS.DAL.Database.proc_APPLICATION_WORKFLOWSLoadAllByARCHIVE_DOCUMENT_TYPESResult selected_workflow = gvApplicationWorkflows.GetFocusedRow() as PRO_DOCS.DAL.Database.proc_APPLICATION_WORKFLOWSLoadAllByARCHIVE_DOCUMENT_TYPESResult;
                this.Text = selected_workflow.APPLICATION_WORKFLOW_NAME + " ...";
                CURRENT_SELECTED_APPLICATION_WORFLOW_ID = selected_workflow.APPLICATION_WORFLOW_ID;

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void frmWorkFlowSelection_Load(object sender, EventArgs e)
        {
            List<PRO_DOCS.DAL.Database.proc_APPLICATION_WORKFLOWSLoadAllByARCHIVE_DOCUMENT_TYPESResult> workflows = PRO_DOCSDatabaseDataContext.proc_APPLICATION_WORKFLOWSLoadAllByARCHIVE_DOCUMENT_TYPES(CURRENT_SELECTED_DOCUMENT_TYPE_ID).ToList();

            if (workflows.Count.Equals(1))
            {
                CURRENT_SELECTED_APPLICATION_WORFLOW_ID = workflows[0].APPLICATION_WORFLOW_ID;

                START_FILE_WORKFLOW();

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else if (workflows.Count > (1))
            {
                gcApplicationWorkFlows.DataSource = workflows;
            }
            else
            {
                //No workflows found
                this.DialogResult = System.Windows.Forms.DialogResult.No;
            }
        }
    }
}